package com.zzptc.myapp1209;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzptc.myapp1209.Service.MyappAllService;
import com.zzptc.myapp1209.entity.Caipu;
import com.zzptc.myapp1209.entity.ResultsBean;

import org.litepal.LitePal;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    List<ResultsBean> allResultsBeanlist; //定义完成

    ListView listView;
    private static final String TAG = "MainActivity";

    Retrofit r=new Retrofit.Builder()
            .baseUrl(" http://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    @Override
    protected void onCreate(Bundle savedInstanceState) {   //把数据存入数据库中
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.lv);

        getCaipuInfo();
        getCaipuInfoFromDB();
        MyAdapter myAdapter=new MyAdapter();
        listView.setAdapter(myAdapter);


    }

    private void getCaipuInfoFromDB() {   //把数据从数据库中取出来
        allResultsBeanlist  = LitePal.findAll(ResultsBean.class);// 有问题 没定义 //刚刚定义完成




    }


    private void getCaipuInfo() {
        //构造接口对象
        MyappAllService myappAllService = r.create(MyappAllService.class);
        //调用接口中的方法 返回call类型
        Call<Caipu>allCaipu=myappAllService.getAllCaipu(5);
        allCaipu.enqueue(new Callback<Caipu>() {
            @Override
            public void onResponse(Call<Caipu> call, Response<Caipu> response) {
                Caipu caipu= response.body();                                   //caipu=r
                Log.d(TAG, "onResponse: "+caipu.getResults().size());
                //将数据放到SQL数据库中去 .使用litepal数据库框架
                List<ResultsBean>results=caipu.getResults();//r=caipu
                for (ResultsBean r:results){
                    r.save();
             //点右下角的DeVice..找到data，再找data，找到com.zzptc.你的包名
                    //再同步 syn..，再database ，找到domoX？，储存到E盘，再找到这个domo，
                    //拖到SQLlite，找到result，点data，看看是否有链接和数据


                }

            }

            @Override
            public void onFailure(Call<Caipu> call, Throwable t) {

            }
        });


    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return allResultsBeanlist.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v= convertView.inflate(MainActivity.this,R.layout.recipemodel_item,null);
            ImageView  iv_pic=v.findViewById(R.id.lv_pic);
            TextView   tv_titel=v.findViewById(R.id.tv_title);
            //赋值
            ResultsBean resultsBean=allResultsBeanlist.get(position);
            String cover=resultsBean.getCover();//图片的地址
            Glide.with(MainActivity.this)
                    .load(cover)
                    .into(iv_pic);
            tv_titel.setText(resultsBean.getTitle());

            return v;
        }
    }
}
